<?php
	/**
	 * @package AmpleHarvest
	 */
	use\ampleharvest\code;
	require_once 'TableHeader.php';

	/**
	 * @brief This class Describes the Query_History Table in the AmpleHarvest Database
	 *
	 * - Revision History
	 *  - 0.1
	 * 	 - <b>Date</b>: 08 AUG 2011
	 * 	 - <b>Author</b>: Randy Blancett
	 * 	  - Initial Version
	 *
	 * @author Randy Blancett
	 * @version 0.1
	 * @ingroup code classes table
	 */
	class cTABLE_Metric extends cTable
	{
		/**
		 * @{
		 * @name Category
		 */
		const C_STR_CATEGORY_HIT_COUNT = "HIT_COUNT";
		const C_STR_CATEGORY_TOTAL_PAGES = "TOTAL_PAGES";
		const C_STR_CATEGORY_TOTAL_USERS = "TOTAL_USERS";

		const C_STR_SUBCATEGORY_HOURLY = "HOURLY";
		const C_STR_SUBCATEGORY_DAILY = "DAILY";
		const C_STR_SUBCATEGORY_WEEKLY = "WEEKLY";
		const C_STR_SUBCATEGORY_MONTHLY = "MONTHLY";
		const C_STR_SUBCATEGORY_YEARLY = "YEARLY";
		const C_STR_SUBCATEGORY_TOTAL = "TOTAL";
		
		const C_STR_SUBCATEGORY_ARTICLE = "ARTICLE";
		
		const C_STR_GROUP_SOLR = "SOLR";
		const C_STR_GROUP_ACTIVE = "ACTIVE";
		const C_STR_GROUP_TOTAL = "TOTAL";
		
		const C_STR_GROUP_INDEX_ERROR = "INDEX_ERROR";//-2
		const C_STR_GROUP_PROCESS_ERROR = "PROCESS_ERROR";//-1
		const C_STR_GROUP_NEW = "NEW";//0
		const C_STR_GROUP_PROCESSING = "PROCESSING";//1
		const C_STR_GROUP_PROCESSED = "PROCESSED";//2
		const C_STR_GROUP_INDEXING = "INDEXING";//3
		const C_STR_GROUP_INDEXED = "INDEXED";//4
		/**
		 * @}
		 */

		/**
		 * @{
		 * @name Columns
		 */
		public $m_int_COL_ID = null;
		public $m_int_COL_DateTime = null;
		public $m_int_COL_Category = null;
		public $m_int_COL_Group= null;
		public $m_int_COL_Subcategory = null;
		public $m_int_COL_Value = null;

		/**
		 * @}
		 */

		/**
		 * This constructor sets all the required column data
		 */
		public function __construct()
		{
			$this->m_str_TableName = "metric";
			$this->set_Engine(self::C_STR_ENGINE_INNODB);
			//$this->set_CharSet(self::C_STR_CHAR_SET_BINARY);
			//$this->set_Collation(self::C_STR_COLLATION_BINARY);

			//Create Column Objects
			$obj_COL_ID = new cColVarChar("id");
			$obj_COL_ID->set_Length(36);
			$obj_COL_ID->set_Index(abs_Column::c_int_INDEX_PRIMARY);
			$obj_COL_ID->set_Comment("This column holds the ID of the Metric Data.");
			$obj_COL_ID->set_Editable(false);
			$obj_COL_ID->set_Label("ID");

			$obj_COL_Category = new cColVarChar("category");
			$obj_COL_Category->set_Length(50);
			$obj_COL_Category->set_Index(abs_Column::c_int_INDEX_INDEX);
			$obj_COL_Category->set_Comment("This column holds the category of the metric.");
			$obj_COL_Category->set_Label("Category");

			$obj_COL_Group = new cColVarChar("group");
			$obj_COL_Group->set_Length(200);
			$obj_COL_Group->set_Index(abs_Column::c_int_INDEX_INDEX);
			$obj_COL_Group->set_Comment("This column holds the group to wich this set of data belongs.");
			$obj_COL_Group->set_Label("group");

			$obj_COL_SubCategory = new cColVarChar("subcategory");
			$obj_COL_SubCategory->set_Length(50);
			$obj_COL_SubCategory->set_Index(abs_Column::c_int_INDEX_INDEX);
			$obj_COL_SubCategory->set_Comment("This column holds the subcategory of the metric.");
			$obj_COL_SubCategory->set_Label("Subcategory");

			$obj_COL_Date = new cColDateTime("datetime");
			$obj_COL_Date->set_Index(abs_Column::c_int_INDEX_INDEX);
			$obj_COL_Date->set_Comment("This column holds the date/time the data was collected.");
			$obj_COL_Date->set_Label("Date Collected");

			$obj_COL_Value = new cColVarChar("value");
			$obj_COL_Value->set_Length(100);
			$obj_COL_Value->set_Comment("This column holds the Value collected.");
			$obj_COL_Value->set_Label("Value");

			//Set up connection
			$this->m_obj_Connection = new cConnection("AmpleHarvest", "admin");

			//Activate all Debug options
			$this->m_obj_Connection->set_DebugLevel(cLog_Connection_Debug::c_int_LEVEL_HIDDEN);
			$this->m_obj_Connection->set_LogLevel(cLog_Connection::c_int_LEVEL_BASIC);
			//$this->m_obj_Connection->activate_Logging();
			//$this->m_obj_Connection->activate_LogDebug();
			//$this->m_obj_Connection->activate_ScreenDebug();

			$this->m_int_COL_ID = $this->add_Column($obj_COL_ID);
			$this->m_int_COL_DateTime = $this->add_Column($obj_COL_Date);
			$this->m_int_COL_Category = $this->add_Column($obj_COL_Category);
			$this->m_int_COL_Group = $this->add_Column($obj_COL_Group);
			$this->m_int_COL_Subcategory = $this->add_Column($obj_COL_SubCategory);
			$this->m_int_COL_Value = $this->add_Column($obj_COL_Value);
		}

		public function insert_DefaultData()
		{

			return true;
		}

		/**
		 * Returns if the id is a valid and existing ID
		 * @param string $str_User_ID ID of the Page to check (GUID)
		 * @return boolean true if valid false if not
		 */
		public function validate_ID($str_ID)
		{
			// ensure that there is an ID passed and that it is a valid GUID
			if (!empty($str_ID) && preg_match(self::c_REG_GUID_VALID, $str_ID))
			{
				$this->clear_Query();
				$this->add_ColumnQuery($this->m_int_COL_ID, $str_ID);
				if ($this->do_Search() > 0)
				{
					return true;
				}
			}
			return false;
		}

		/**
		 * Loads information into object from Database based on ID passed
		 * @param string $str_ID ID of the id to check (int)
		 * return boolean true if loaded false if not
		 */
		public function load_DataFromID($str_ID)
		{
			$this->clear();

			if ($this->validate_ID($str_ID))
			{
				$this->clear_Query();
				$this->add_ColumnQuery($this->m_int_COL_ID, $str_ID);
				if ($this->do_Search() > 0)
				{
					$obj_RS = $this->get_ResultSet();
					if ($obj_RS[0])
					{
						$this->load_DataFromArray($obj_RS[0]);
						return true;
					}
				}
			}
			return false;
		}
		public function insert_Data2DB()
		{
			if (!$this->get_id())
			{
				$this->set_id($this->create_GUID());
			}

			if (!$this->get_datetime())
			{
				$obj_Date = new DateTime();
				$this->set_datetime($obj_Date->format("Y-m-d H:i:s"));
			}

			return parent::insert_Data2DB();
		}

		/**
		 * Saves Information to the Database
		 */
		public function save_Data2DB()
		{
			if ($this->validate_ID($this->get_id()))
			{
				return $this->update_Data2DB();
			}
			else
			{
				return $this->insert_Data2DB();
			}
		}
	}
?>
